class StatsController < ApplicationController
  caches_page :per_day
  caches_page :per_month
  caches_page :per_authority
  caches_page :per_day_of_week

  def index
    summary
    render :action => 'summary'
  end
  
  def summary
    @graph = open_flash_chart_object(600,300, '/stats/per_day', false) 
    @months = open_flash_chart_object(600,300, '/stats/per_month', false)
    @days= open_flash_chart_object(600,300,'/stats/per_day_of_week', false)
    @authorities=open_flash_chart_object(600,600,'/stats/per_authority',false)
  end

  def per_day
    @item_counts=[]
    @date_labels=[]
    1.upto(30) do |i|
      t=Time.now.at_midnight-60*60*24*i
      @item_counts[30-i]=Item.date_count(t)
      @date_labels[30-i]=(Date.today-i).to_s 
    end
    g = Graph.new
    g.title("Posts per Day", '{font-size: 26px;}')
    g.set_data(@item_counts)
    g.line(2,'0x80a033','Posts', 10)
    g.set_x_labels(@date_labels)
    g.set_x_label_style( 10, '#CC3399', 2 );
    g.set_y_max(200)
    g.set_y_label_steps(8)
    render :text => g.render
  end
  
  def per_month
    @item_counts=[]
    @date_labels=[]
    now=DateTime.now
    2007.upto(now.year) do |i|
      1.upto(12) do |j|
        if i<now.year || j<now.month
          @item_counts<<Item.month_count(i,j)
          @date_labels <<DateTime.new(i,j,1).strftime("%b %Y")
        end
      end
    end
    g = Graph.new
    g.title("Posts per Month", '{font-size: 26px;}')
    g.set_data(@item_counts)
    g.line(2,'0x80a033','Posts', 10)
    g.set_x_labels(@date_labels)
    g.set_x_label_style( 10, '#CC3399', 2 )
    @top=@item_counts.max/250+1
    g.set_y_max(@top*250)
    g.set_y_label_steps(@top)
    render :text => g.render
  end

  def per_day_of_week
    @item_counts=[]
    1.upto(7) do |i|
      @item_counts<<Item.day_of_week_count(i)
    end
    g = Graph.new
    g.pie(60, '#505050', '{font-size: 12px; color: #404040;}')
    g.pie_values(@item_counts, %w(Saturday Sunday Monday Tuesday Wednesday Thursday Friday))
    g.pie_slice_colors(%w(#99FF66 #CC6699 #FF9966 #339999))
    g.set_tool_tip("#val#")
    g.title("Posts by Day of the Week", '{font-size:18px; color: #d01f3c}' )
    render :text => g.render
  end
  
  def per_authority
    @labels=[]
    bar1 = Bar.new(50, '#0066CC')
    bar1.key('Posts', 10)
    Authority.find(:all).each do |auth|
      bar1.data << Item.authority_count(auth.id)
      @labels << auth.name.chomp("Council")
    end
    g=Graph.new
    g.title("Posts per Authority", "{font-size: 26px;}")
    g.data_sets << bar1
    g.set_x_labels(@labels)
    g.set_x_label_style(10, '#9933CC', 2)
    g.set_x_axis_steps(1)
    g.set_y_max(4000)
    g.set_y_label_steps(40)
    g.set_y_legend("Posts", 12, "0x736AFF")
    render :text => g.render
  end

  def hourly
    @item_counts=[]
    @date_labels={}
    0.upto(23) do |i|
      @item_counts[i]=Item.time_of_day_count(i)
      @date_labels[i]=i.to_s
    end
    g = Gruff::Bar.new(600)
    g.title = "Posts by Time of Day" 
    #g.replace_colors(['#FA0', '#d9e043', '#34d8a2'])
#     g.theme = {
#                :colors => %w(green white red),
#                :marker_color => 'black',
#                :background_colors => %w(white grey)
#                }
    #g. theme_rails_keynote()  
    #g.marker_font_size=15
    g.data("Posts", @item_counts)
    #g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}
    g.labels=@date_labels
    g.write('images/hour_stats.png')
    
  end

  
end
